﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace UCNKompetenceBors
{
    public partial class Site : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string culture = String.Empty;

                if (HttpContext.Current.Session["MyCulture"] != null)
                    culture = HttpContext.Current.Session["MyCulture"].ToString();

                if (!String.IsNullOrEmpty(culture))
                {
                    GeneralTools.SetCulture(culture);
                }

                LocalizeControls();
                BindLoginNameNavigateUrl();
                FocusMenu();
            }
        }

        /// <summary>
        /// Binds the NavigateUrl property of the profile link.
        /// </summary>
        private void BindLoginNameNavigateUrl()
        {
            MembershipUser mu = Membership.GetUser();

            if (mu != null)
            {
                hyLinkLoginName.NavigateUrl = "~/User/profile.aspx?uid=" + mu.ProviderUserKey.ToString();
            }
        }

        /// <summary>
        /// Sets the Selected property of the MenuItem corresponding to the currently selected site.
        /// </summary>
        private void FocusMenu()
        {
            int i = 0;
            bool found = false;

            while (!found && i < mainMenu.Items.Count)
            {
                if (Page.Title == mainMenu.Items[i].Value)
                {
                    mainMenu.Items[i].Selected = true;
                    found = true;
                }
                i++;
            }
        }

        /// <summary>
        /// Localizes the controls of the site, based on the current UI Culture.
        /// </summary>
        private void LocalizeControls()
        {
            btnSimpleSearch.Text = GetLocalResourceObject("btnSimpleSearchText").ToString();
            
            foreach (MenuItem mi in mainMenu.Items)
            {
                switch (mi.Value)
                {
                    case "Forside":
                        mi.Text = GetLocalResourceObject("menuItemHomeText").ToString();
                        break;
                    case "Nyheder":
                        mi.Text = GetLocalResourceObject("menuItemNewsText").ToString();
                        break;
                    case "Køb":
                        mi.Text = GetLocalResourceObject("menuItemPurchaseText").ToString();
                        break;
                    case "Salg":
                        mi.Text = GetLocalResourceObject("menuItemSaleText").ToString();
                        break;
                    case "Om os":
                        mi.Text = GetLocalResourceObject("menuItemAboutText").ToString();
                        break;
                    default:
                        break;
                }
            }

            if (!this.Page.User.Identity.IsAuthenticated)
            {
                HyperLink hyLinkSignup = (HyperLink)LoginViewBanner.FindControl("hyLinkSignup");
                hyLinkSignup.Text = GetLocalResourceObject("hyLinkSignupText").ToString();
            }
            else if (Roles.GetRolesForUser()[0].Equals("administrator"))
            {
                HyperLink hyLinkAdmin = (HyperLink)LoginViewBanner.FindControl("hyLinkAdmin");
                hyLinkAdmin.Text = GetLocalResourceObject("hyLinkAdminText").ToString();
            }

            LoginStatus.LoginText = GetLocalResourceObject("LoginStatusLoginText").ToString();
            LoginStatus.LogoutText = GetLocalResourceObject("LoginStatusLogoutText").ToString();
        }

        // Sets the Current UI Culture to "da-DK" and calls the LocalizeControls() method
        // Finally it redirects back to the exact same URL
        protected void imgBtnDanish_Click(object sender, ImageClickEventArgs e)
        {
            GeneralTools.SetCulture("da-DK");
            LocalizeControls();

            string url = String.Empty;

            if (!Request.QueryString.ToString().Equals(""))
                url += Request.Path + "?" + Request.QueryString.ToString();
            else
                url += Request.Path;

            Response.Redirect(url);
        }

        // Sets the Current UI Culture to "en-GB" and calls the LocalizeControls() method
        // Finally it redirects back to the exact same URL
        protected void imgBtnEnglish_Click(object sender, ImageClickEventArgs e)
        {
            GeneralTools.SetCulture("en-GB");
            LocalizeControls();
            
            string url = String.Empty;

            if (!Request.QueryString.ToString().Equals(""))
                url += Request.Path + "?" + Request.QueryString.ToString();
            else
                url += Request.Path;

            Response.Redirect(url);
        }
    }
}